<!-- form-login -->
<template>
    <div class="form-login">
        <div class="top">
            <div class="form-item">
                <div class="label">用户</div>
                <div class="login-input">
                    <input type="text" v-model="username">
                </div>
                <div v-show="showUser" class="login-error">用户不能为空</div>
            </div>
            <div class="form-item form-item1">
                <div class="label">密码</div>
                <div class="login-input">
                    <input type="password" v-model="pwd">
                </div>
                <div v-show="showPwd" class="login-error">密码不能为空</div>
            </div>

            <el-button 
            :loading="loging"
            @click="handleLogin"
            class="login-btn">登录</el-button>
        </div>
        <div class="bottom">
            <template v-for="(person, index) in personList">
                <el-radio 
                :key="index"
                @change="getChangeVal"
                class="selectRadio"
                v-model="radio1" 
                :label="person.username" 
                >{{person.username}}</el-radio>
            </template>
        </div>
    </div>
</template>

<script>
import {
    getUserList,
    doLogin
} from '@/apis'
export default {
    name: "form-login",
    data() {
        return {
            username: '',
            pwd: '',
            loging: false,
            showUser: false,
            showPwd: false,
            radio1: '1',
            // 人员列表
            personList: []
        };
    },
    watch: {
        username (newVal) {
            this.showUser = newVal.length == 0 ? true : false
        },
        pwd (newVal) {
            this.showPwd = newVal.length == 0 ? true : false
        },
    },
    methods:{
        // 点击登录按钮
        handleLogin () {
            if (!this.username) {
                this.showUser = true
            } 

            if (!this.pwd) {
                this.showPwd = true;
                return
            } 
            this.loging = true;
            this.requestLogin();
        },

        // 根据工区 获取人员列表
        async getAllUser () {
            try {
                //let id = 99;
                let id = window.localStorage.getItem('organizationId');
                let res = await getUserList(id);
                console.log('获取人员列表', res);
                let {
                    code,
                    msg,
                    data
                } = res.data;
                if (code == 200) {
                    this.personList = data;
                    let per = data ? JSON.stringify(data) : null;
                    window.sessionStorage.setItem('per', per);
                } else if (code == -2) {
                    this.$message({
                        type: 'error',
                        message: `${msg}`
                    })
                    this.$store.commit('CLEAR_LOGIN');
                    this.$router.push('/login').catch({});    
                } else {
                    this.$message({
                        type: 'error',
                        message: `${msg}`
                    })
                }
            } catch(e) {
                console.log('获取人员列表',e)
            }
        },

        // 点击选择人员
        getChangeVal (val) {
            this.username = val;
        },

        // 登录功能
        async requestLogin () {
            try {
                let params = {
                    "password": this.pwd.trim(),
                    "username": this.username.trim()
                }
                let res = await doLogin(params);
                console.log('登录功能', res);
                let {
                    code,
                    msg,
                    data
                } = res.data;
                if (code == 200) {
                    this.loging = false;
                    this.$store.commit('SET_USER', this.username.trim());
                    window.sessionStorage.setItem('token', data);
                    if (this.$route.query.redirect) { //如果存在参数
                        let redirect = this.$route.query.redirect
                        this.$router.push(redirect).catch({});//则跳转至进入登录页前的路由
                    } else {
                        this.$router.push('/home').catch({});
                    }
                } else if (code == -2) {
                    this.$message({
                        type: 'error',
                        message: `${msg}`
                    })
                    this.$store.commit('CLEAR_LOGIN');
                    this.$router.push('/login').catch({});    
                } else {
                    this.$message({
                        type: 'error',
                        message: `${msg}`
                    })
                    this.loging = false;
                }
            } catch(e) {
                this.loging = false;
                console.log('requestLogin',e)
            }
        }, 
    },
    created () {
        this.getAllUser();
    }

};
</script>
<style lang='scss'>
.form-login {
    width: 100%;
    height: 100%;
    box-sizing: border-box;

    .top {
        width: 100%;
        height: 78.2%;
        padding-top: 4.8%;
        box-sizing: border-box;
        overflow: auto;
    }

    .bottom {
        width: 100%;
        height: 21.8%;
        background: #202E45;
        box-sizing: border-box;
        overflow: scroll;
        box-shadow: inset -1px 0px 0px 0px rgba(67,88,122,1);

        .selectRadio {
            width: 213px;
            height: 37.25%;
            margin: 1.37% 0 0 1.13%;
            background: url('../../assets/button_nor.png') no-repeat center center;
            background-size: 100% 100%;
            float: left;

            .el-radio__input {
                display: none!important;
            }

            .el-radio__label {
                width: 213px;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 32px;
                color: #FFFFFF;
                letter-spacing: 0;
                font-weight: 400;
            }
        }

        .is-checked {
            background: url('../../assets/button_act.png') no-repeat center center;
            background-size: 100% 100%;
        }
    }

    .form-item {
        width: 590px;
        margin: 0 auto;
        margin-top: 3.5%;
        position: relative;
        
        .label {
            font-size: 32px;
            color: #FFFFFF;
            letter-spacing: 0;
            font-weight: 400;
        }

        .login-input {
            width: 100%;
            height: 80px;
            box-sizing: border-box;
            background: rgba(38,131,255,0.08);
            border: 1px solid #2683FF;
            border-radius: 4px;
            position: relative;
            margin-top: 1.45%;

            &::before {
                content: '';
                position: absolute;
                width: 1px;
                height: 60px;
                background: #2683FF;
                top: 10px;
                left: 72px;
            }

            &::after {
                content: '';
                position: absolute;
                width: 30px;
                height: 30px;
                background: url('../../assets/user.png') no-repeat center center;
                background-size: 100% 100%;
                top: 25px;
                left: 25px;
            }

            input {
                position: absolute;
                top: 0;
                right: 12px;
                width: 488px;
                height: 80px;
                outline: none;
                border: none;
                background: transparent;
                font-size: 32px;
                color: #FFFFFF;
                letter-spacing: 0;
                font-weight: 400;
            }
        }

        .login-error {
            position: absolute;
            bottom: -35px;
            left: 0;
            font-size: 16px;
            color: #f40;
            line-height: 34px;
        }
    }
    
    .form-item1 {
        .login-input {
            &::after {
                content: '';
                position: absolute;
                width: 30px;
                height: 30px;
                background: url('../../assets/pwd.png') no-repeat center center;
                background-size: 100% 100%;
                top: 25px;
                left: 25px;
            }
        }
    }

    .login-btn {
        display: block;
        margin: 0 auto;
        margin-top: 8.8%;
        width: 590px;
        height: 80px;
        background: #2683FF;
        border-radius: 4px;
        text-align: center;
        line-height: 80px;
        font-size: 32px;
        color: #FFFFFF;
        letter-spacing: 0;
        font-weight: 400;
        border: none;
        padding: 0;
    }
}
</style>